/*
 *  isfiledialog.c
 *  sfxr
 *
 *  Created by Ivan Safrin on 4/11/08.
 *  Copyright 2008 __MyCompanyName__. All rights reserved.
 *
 */

#if defined(__APPLE__) && defined(__MACH__)
	#include <Carbon/Carbon.h>	
#else

#endif

#include "isfiledialog.h"

int ISSaveDialog(char *filename, char *initialFilename, char *extension) {
	int selected = 0;
	char temp[1024] = {0};
	
	// -----------------------------------------------------------------------------------------
	// [ OSX ] ---------------------------------------------------------------------------------
	// -----------------------------------------------------------------------------------------
	
	#if defined(__APPLE__) && defined(__MACH__)
	OSStatus status = noErr;

	NavDialogCreationOptions navOptions;
	status = NavGetDefaultDialogCreationOptions(&navOptions);
	
	if(initialFilename != NULL) {
		if(strlen(initialFilename) > 0)
			strcpy(temp, initialFilename);
		else
			strcpy(temp, "untitled");
	} else {
		strcpy(temp, "untitled");
	}
	strcat(temp, ".");
	strcat(temp, extension);

	CFMutableStringRef newFileName = CFStringCreateWithCString(NULL, temp, kCFStringEncodingASCII);
	
	navOptions.preferenceKey = 2;
	navOptions.modality = kWindowModalityAppModal;
	navOptions.parentWindow = FrontWindow();
	navOptions.optionFlags |= kNavPreserveSaveFileExtension;
	navOptions.saveFileName = newFileName;
	
	NavDialogRef theDialog = NULL;
	status = NavCreatePutFileDialog(&navOptions, kNavGenericSignature, kNavGenericSignature, NULL, FrontWindow(), &theDialog);
	status = NavDialogRun(theDialog);
	
	if(NavDialogGetUserAction(theDialog) != kNavUserActionCancel) {
		NavReplyRecord reply;
		FSRef fileAsFSRef;
		CFURLRef fileAsCFURLRef = NULL;
		NavDialogGetReply(theDialog, &reply);
		status = AEGetNthPtr(&reply.selection, 1, typeFSRef, NULL, NULL, &fileAsFSRef, sizeof(FSRef), NULL);
		fileAsCFURLRef = CFURLCreateFromFSRef(NULL, &fileAsFSRef);
		CFStringRef file = CFURLCopyPath(fileAsCFURLRef);
		memset(temp,0,1024);
		CFStringGetCString(file,temp,1024,kCFStringEncodingASCII);
		strcpy(filename, temp);
		memset(temp,0,1024);
		CFStringGetCString(reply.saveFileName,temp,1024,kCFStringEncodingASCII);
		strcat(filename, temp);
		NavDisposeReply(&reply);
		selected = 1;
	} else {
		selected = 0;
	}
	
	if (theDialog != NULL) 
		NavDialogDispose(theDialog);

	#else

	#endif

	return selected;
}

int ISOpenDialog(char *filename) {
	int selected = 0;
	char temp[1024] = {0};
	
	// -----------------------------------------------------------------------------------------
	// [ OSX ] ---------------------------------------------------------------------------------
	// -----------------------------------------------------------------------------------------
	
	#if defined(__APPLE__) && defined(__MACH__)
	OSStatus status = noErr;

	NavDialogCreationOptions navOptions;
	status = NavGetDefaultDialogCreationOptions(&navOptions);

	navOptions.preferenceKey = 2;
	navOptions.modality = kWindowModalityAppModal;
	navOptions.parentWindow = FrontWindow();
	
	NavDialogRef theDialog = NULL;
	status = NavCreateGetFileDialog(&navOptions, NULL, NULL, NULL, NULL, FrontWindow(), &theDialog);
	status = NavDialogRun(theDialog);
	

	if(NavDialogGetUserAction(theDialog) != kNavUserActionCancel) {
		NavReplyRecord reply;
		FSRef fileAsFSRef;
		CFURLRef fileAsCFURLRef = NULL;
		NavDialogGetReply(theDialog, &reply);
		status = AEGetNthPtr(&reply.selection, 1, typeFSRef, NULL, NULL, &fileAsFSRef, sizeof(FSRef), NULL);
		fileAsCFURLRef = CFURLCreateFromFSRef(NULL, &fileAsFSRef);
		CFStringRef file = CFURLCopyPath(fileAsCFURLRef);
		memset(temp,0,1024);
		CFStringGetCString(file,temp,1024,kCFStringEncodingASCII);
		strcpy(filename, temp);
		NavDisposeReply(&reply);
		printf("open: %s\n", filename);
		selected = 1;
	} else {
		selected = 0;
	}
	
	if (theDialog != NULL) 
		NavDialogDispose(theDialog);

	#else

	#endif

	return selected;
}